﻿CREATE PROCEDURE [dbo].[ApplySubscription]
	@ServiceName servicenametype ,
	@Events ApplyEventSubscriptions READONLY
AS
BEGIN
	BEGIN TRANSACTION

	IF NOT EXISTS (SELECT TOP 1 1 FROM [Subscription] WHERE [ServiceName] = @ServiceName)
	BEGIN
		EXEC AddSubscriptionInfrastructure @ServiceName = @ServiceName	
	END

	DELETE FROM [Subscription] WHERE ServiceName = @ServiceName
	
	INSERT INTO [Subscription] ([ServiceName],[EventName])
	SELECT [ServiceName], [EventName]
	FROM @Events
	
	COMMIT TRANSACTION
END